package com.macro.mall.portal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.macro.mall.portal.service.ILoanPackageService;
import com.macro.mall.portal.dao.LoanPackageMapper;
import com.macro.mall.portal.domain.LoanPackage;
import com.macro.mall.portal.util.SplitArrayUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 我能贷多少记录表 服务实现类
 * </p>
 *
 * @author db
 * @since 2021-07-04
 */
@Service
public class LoanPackageServiceImpl extends ServiceImpl<LoanPackageMapper, LoanPackage> implements ILoanPackageService {
    @Autowired
    private LoanPackageMapper loanPackageMapper;

    @Override
    public List<LoanPackage> selectList(String openId, Integer page, Integer size) {
        if (!StringUtils.isEmpty(openId)) {
            List<LoanPackage> packageList = loanPackageMapper.selectList(null);
            List<LoanPackage> randomList = SplitArrayUtils.getRandomList(packageList, 9);
            return randomList;
        }
        PageHelper.startPage(page, size);
        List<LoanPackage> loanPackages = loanPackageMapper.selectList(null);
        return loanPackages;
    }

    @Override
    public LoanPackage getInfo(String id) {
        JSONObject object = new JSONObject(id);
        int anInt = object.getInt("id");
        LoanPackage loanPackage = loanPackageMapper.selectById(anInt);
        return loanPackage;
    }

    @Override
    public List<LoanPackage> getBatchInfo(LoanPackage loanPackage) {
        List<Integer> list = new ArrayList<>();
        loanPackage.getIds().stream().forEach(item -> {
            list.add(Integer.parseInt(item));
        });
        List<LoanPackage> loanPackages = loanPackageMapper.selectBatchIds(list);
        return loanPackages;
    }


}
